package com.prs.crm.util.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Element;
import org.xml.sax.SAXException;

import com.prs.crm.util.GanttExport;
import com.prs.crm.vo.gantt.GanttTask;
import com.prs.crm.vo.gantt.PredecessorTask;


/**
 * 甘特图导出实现
 * @author prs.chenzhi
 *
 */
public class GanttExportImpl implements GanttExport{
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	
	/**
	 * 导出
	 * @see com.prs.crm.util.impl.GanttExport@exportXML
	 */
	public org.w3c.dom.Document exportXML(GanttTask task)
	{
		try
		{
			org.dom4j.Document doc = new org.dom4j.dom.DOMDocumentFactory().createDocument();
			Element root = doc.addElement("Project","http://schemas.microsoft.com/project");
			Element tasks = root.addElement("Tasks");
			addTask(tasks,task,null,0);
			return XmlString2W3cDom(doc.asXML());
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	private void addTask(Element parent,GanttTask task,String prefix,int sequence)
	{
		Element tastnode = parent.addElement("Task");
		tastnode.addElement("UID").setText(task.getUid().toString());
		tastnode.addElement("ID").setText(task.getUid().toString());
		tastnode.addElement("Name").setText(task.getName());
		String outlinenumber = (prefix!=null)?prefix+"."+(sequence):String.valueOf((sequence));
		tastnode.addElement("OutlineNumber").setText(outlinenumber);
		tastnode.addElement("Start").setText(convertXMLDate(task.getStart()));
		tastnode.addElement("Finish").setText(convertXMLDate(task.getFinish()));
		if (task.getConstraintType()!=null)
		{
			tastnode.addElement("ConstraintType").setText(task.getConstraintType().toString());
			if (task.getConstraintType()>1 && task.getConstraintDate()!=null)
				tastnode.addElement("ConstraintDate").setText(convertXMLDate(task.getConstraintDate()));
		}
		if (task.getLinkUrl()!=null)
			tastnode.addElement("HyperlinkAddress").setText(task.getLinkUrl());
		if (task.isSummary())
			tastnode.addElement("Summary").setText("1");
		else
			tastnode.addElement("Summary").setText("0");
		if (task.isMilestone())
			tastnode.addElement("Milestone").setText("1");
		else
			tastnode.addElement("Milestone").setText("0");
		if (task.getPercentComplete()!=null)
			tastnode.addElement("PercentComplete").setText(task.getPercentComplete().toString());
		else
			tastnode.addElement("PercentComplete").setText("0");
		if (task.getBaseLine()!=null)
		{
			Element baseLine = tastnode.addElement("BaseLine");
			baseLine.addElement("Start").setText(convertXMLDate(task.getBaseLine().getStart()));
			baseLine.addElement("Finish").setText(convertXMLDate(task.getBaseLine().getFinish()));
		}
		if (task.getPredecessorTasks()!=null)
		{
			for (PredecessorTask ptask : task.getPredecessorTasks())
			{
				Element prelink = tastnode.addElement("PredecessorLink");
				prelink.addElement("PredecessorUID").setText(ptask.getPredecessorUID().toString());
				prelink.addElement("Type").setText(ptask.getType().toString());
			}
		}
		if (task.getChildrens()!=null)
		{
			if (task.getUid()<1)
				outlinenumber=null;
			for (int i=0;i<task.getChildrens().size();i++)
			{
				this.addTask(parent, task.getChildrens().get(i), outlinenumber, i+1);
			}
		}
	}
	
	
	
	private String convertXMLDate(Date date)
	{
		return sdf.format(date);
	}
	
	public  org.w3c.dom.Document XmlString2W3cDom(String xml) throws SAXException, IOException, ParserConfigurationException {
		
		byte[] byteArray = xml.getBytes("UTF-8");
		
		ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		org.w3c.dom.Document doc = factory.newDocumentBuilder().parse(baos);
		
		return doc;

	}

}
